package com.rongshu.sendmessage.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rongshu.sendmessage.R;
import com.rongshu.sendmessage.base.BaseActivity;
import com.rongshu.sendmessage.base.MyApplication;
import com.rongshu.sendmessage.util.ClientPublicParamUtil;
import com.rongshu.sendmessage.util.StringUtils;
import com.rongshu.sendmessage.util.ToastUtils;
import com.rongshu.sendmessage.util.downTime.MCountDownTimer;
import com.rongshu.sendmessage.util.httpUtil.HYContent;
import com.rongshu.sendmessage.util.httpUtil.IModel;

import java.util.HashMap;
import java.util.Map;

public class BindPhoneActivity extends BaseActivity {

    private EditText et_phone, et_new_code;
    private MCountDownTimer countDownTimer;
    private Button request_code;
    private boolean isClick=true;

    private String userId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
        setContentView(R.layout.activity_bind_phone);
        initView();
        userId= MyApplication.mSettings.getString(HYContent.UserId,"");
        countDownTimer = new MCountDownTimer();
        this.countDownTimer.setOnCountDownListener(new MCountDownTimer.OnCountDownListener() {
            @Override
            public void onStop() {
                if (request_code != null) {
                    request_code.setText("获取验证码");
                    isClick=true;
                }
            }

            @Override
            public void onTick(long second) {
                if (request_code != null) {
                    request_code.setText("获取验证码");
                    String time = "发送中(%s)";
                    time = String.format(time, second);
                    request_code.setText(time);
                    isClick=false;
                }
            }
        });
    }

    private void initView() {
        TextView title = findViewById(R.id.title);
        title.setText("更换绑定手机号");
        request_code=findViewById(R.id.request_code);
        et_phone = findViewById(R.id.et_phone);
        et_new_code = findViewById(R.id.et_new_code);
        LinearLayout titleLayLeft = findViewById(R.id.titleLayLeft);
        titleLayLeft.setOnClickListener(view->finish());
        request_code.setOnClickListener(view->requestCode());
        findViewById(R.id.btn_submit).setOnClickListener(view->yanzhengCode());
    }
    private void yanzhengCode() {
        if (et_new_code.getText().length()<=2){
            ToastUtils.show(mContext,"请输入正确的验证码");
            return;
        }
        yanzhengByHttp(et_new_code.getText().toString());
    }

    private void yanzhengByHttp(String code) {
        Map<String, String> param = new HashMap<String, String>();
        param.put("userId",userId);
        param.put("validateCode",code);
        param.put("clientPublicParam", ClientPublicParamUtil.getPublicParams(mContext));
        String url = new HYContent().NEW_PHONE_YANZHENG_CODE_URL;
        OkGo.<String>post(url).params(param).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                IModel.AjaxResult jr = new IModel.AjaxResult(response.body());
                final int code = jr.getCode();
                if (code>0){
                    submit();
                }else{
                    ToastUtils.show(mContext,jr.getMsg());
                }
            }
        });
    }

    private void submit() {
        Map<String, String> param = new HashMap<String, String>();
        param.put("userId",userId);
        param.put("clientPublicParam", ClientPublicParamUtil.getPublicParams(mContext));
        String url = new HYContent().SUBMIT_PHONE_YANZHENG_CODE_URL;
        OkGo.<String>post(url).params(param).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                IModel.AjaxResult jr = new IModel.AjaxResult(response.body());
                final int code = jr.getCode();
                if (code>0){
                    setResult(1000);
                    finish();
                }else{
                    ToastUtils.show(mContext,jr.getMsg());
                }
            }
        });
    }

    private void requestCode() {
        if(!isClick){
            return;
        }
        String account = et_phone.getText().toString();
        if (StringUtils.isEmpty(account)) {
            ToastUtils.show(mContext, "手机号码不能为空！");
            return;
        }
        getVCode(account);
    }

    private void getVCode(String account) {
        Map<String, String> param = new HashMap<String, String>();
        param.put("userId",userId);
        param.put("newPhone",account);
        param.put("clientPublicParam", ClientPublicParamUtil.getPublicParams(mContext));
        String url = new HYContent().NEW_PHONE_CODE_URL;
        OkGo.<String>post(url).params(param).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                IModel.AjaxResult jr = new IModel.AjaxResult(response.body());
                final int code = jr.getCode();
                if (code>0){
                    countDownTimer.cancel();
                    countDownTimer.start();
                    ToastUtils.show(mContext,"发送验证码成功！");
                }else{
                    ToastUtils.show(mContext,jr.getMsg());
                }
            }
        });
    }
}